﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using System;
using System.Windows.Forms;
using XCI.Windows.Extensions;
using XCI.Windows.Controls;
using XCI.Windows.Forms;
using XCI.Windows.Helper;

namespace XCI.Windows.Plugin.GridSetting.UI
{
    public partial class TreeConfigForm : FrmBase
    {
        public TreeConfigForm()
        {
            InitializeComponent();

        }

        private DynamicLoadPage tabLoad;
        /// <summary>
        /// 选项卡按需加载
        /// </summary>
        public DynamicLoadPage TabLoad => tabLoad ?? (tabLoad = new DynamicLoadPage(LoadAction));

        protected CaptionControl FormCaptionControl => captionControl1;

        private TreeSettingPlugin Plugin { get; set; }

        public void Initialize(TreeSettingPlugin plugin)
        {
            this.Plugin = plugin;
            TabLoad.LoadPage(xtraTabControl1.SelectedTabPage.Name);
            xtraTabControl1.SelectedPageChanging += (o, e) =>
            {
                TabLoad.LoadPage(e.Page.Name);
            };
        }

        /// <summary>
        /// 选项卡加载处理函数
        /// </summary>
        /// <param name="name">选项卡名称</param>
        protected void LoadAction(string name)
        {
            switch (name)
            {
                case "tabGeneral":
                    gridGeneral1.Initialize(Plugin);
                    break;
                case "tabAppearance":
                    gridAppearance1.Initialize(Plugin.Tree.Appearance);
                    break;
                case "tabExpression":
                    gridExpression1.Initialize(Plugin);
                    break;
                case "tabColumns":
                    gridColumns1.Initialize(Plugin);
                    break;
                case "tabTemplate":
                    gridTemplate1.Initialize(Plugin);
                    break;
                case "tabAdvanced":
                    gridAdvanced1.Initialize(Plugin.Target);
                    break;
            }
        }

        private void GridConfigForm_Load(object sender, EventArgs e)
        {
            tabGeneral.BackColor = tabAdvanced.BackColor = this.BackColor;
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要重置表格配置吗？重置后此表格的配置会全部删除") != DialogResult.Yes)
            {
                return;
            }

            GridSettingManager.Instance
                              .DeleteSetting(Plugin.Id)
                              .EnsureSuccess();
            Plugin.AutoBuildColumn();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            GridSettingManager.Instance
                              .SaveSetting(Plugin.Id, Plugin.GetSetting())
                              .EnsureSuccess();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void btnRestoreDefault_Click(object sender, EventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要恢复表格默认配置吗？恢复会丢失当前配置") != DialogResult.Yes)
            {
                return;
            }

            var data = GridSettingManager.Instance.LoadDefaultSetting(Plugin.Id);
            if (data != null)
            {
                Plugin.Tree.Columns.Clear();
                Plugin.Tree.FormatConditions.Clear();
                Plugin.Tree.Appearance.Reset();
                Plugin.SetSetting(data);
                Plugin.Tree.ViewInfo.PaintAppearance.HideSelectionRow.BackColor = Plugin.Tree.ViewInfo.PaintAppearance.FocusedRow.BackColor;
                GridSettingManager.Instance
                                  .SaveSetting(Plugin.Id, data)
                                  .EnsureSuccess();
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }

        }

        private void btnSaveDefault_Click(object sender, EventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要把当前配置设为默认配置吗？") != DialogResult.Yes)
            {
                return;
            }
            GridSettingManager.Instance.SaveDefaultSetting(Plugin.Id, Plugin.GetSetting()).EnsureSuccess();
            MessageBoxHelper.ShowMessage("默认配置设置成功");


            //this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}